/**
 *@brief: socket's Client
**/

#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define SERVER_PORT			8888
#define SERVER_IP			"192.168.1.13"

int main()
{
	int skt_fd = 0;
	int ret = 0;
	struct sockaddr_in server_sock = {0};
	char client_buf[512] = {0};
	
	//1. 创建服务器的套接字(socket())
	//IPv4--TCP
	skt_fd = socket(AF_INET, SOCK_STREAM,0);
	if(skt_fd < 0)
	{
		perror("Socket create failed!");
		exit(-1);
	}
	
	
	//2. 调用connect 进行连接服务器
	server_sock.sin_family = AF_INET;
	//client_sock.sin_addr = htonl(INADDR_ANY); //客户端要连接的 IP 地址 
	inet_pton(AF_INET, SERVER_IP, &(server_sock.sin_addr));
	server_sock.sin_port = htons(SERVER_PORT); //客户端要连接的端口为8888
	
	ret = connect(skt_fd, (struct sockaddr *)&(server_sock), sizeof(server_sock));
	if(ret < 0)
	{
		perror("Socket connect failed!");
		exit(-1);
	}
	
	printf("服务器连接成功...\n\n");
	
	//while()循环，不断发送给服务器请求
	while(1)
	{
		bzero(client_buf, sizeof(client_buf));
		
		printf("Please input some data:\n");
		fgets(client_buf, sizeof(client_buf) ,stdin);
		ret = send(skt_fd, client_buf, sizeof(client_buf), 0);
		
		if(ret < 0)
		{
			perror("client send failed!");
			exit(-1);
		}
		if(strncmp(client_buf, "exit", 4) == 0)
		{
			break;
		}		
	}
	close(skt_fd);
	return 0;
}
